package cn.myfirefly.core;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;

import cn.myfirefly.core.model.BaseDto;
import cn.myfirefly.core.model.Identity;
import cn.myfirefly.core.model.ResultDto;
import cn.myfirefly.utils.CustomizedPropertyPlaceholderConfigurer;

public class BaseController {
	
	protected final String picRealPath =  (String) CustomizedPropertyPlaceholderConfigurer.getContextProperty("picRealPath");
	protected final String picUrl = (String) CustomizedPropertyPlaceholderConfigurer.getContextProperty("picUrl");

	@Autowired
	private HttpServletRequest request;
	
    public ResultDto success() {
    	return success("执行成功！", null);
    }
    
    @SuppressWarnings({ "rawtypes" })
	public ResultDto success(List data) {
    	return success("执行成功！", data);
    }
    
    public ResultDto success(Object data) {
    	List<Object> list = new ArrayList<Object>();
    	list.add(data);
    	return success("执行成功！", list);
    }
    
    public ResultDto success(String message) {
    	return success(message, null);
    }
    
    @SuppressWarnings({ "rawtypes", "unchecked" })
    public ResultDto success(String message, List data) {
    	ResultDto resultDto = new ResultDto();
    	resultDto.setStatusCode(Const.StatusCode.SUCCESS);
    	resultDto.setResultCode(Const.ResultCode.SUCCESS);
    	resultDto.setMessage(message);
    	resultDto.setData(data);
    	request.setAttribute("write_log", "W");
    	return resultDto;
    }

    public ResultDto success(String message, Object data) {
    	List<Object> list = new ArrayList<Object>();
    	list.add(data);
    	return success(message, list);
    }
    
    public ResultDto error(String message) {
    	ResultDto resultDto = new ResultDto();
    	resultDto.setStatusCode(Const.StatusCode.FAILURE);
    	resultDto.setResultCode(Const.ResultCode.FAILURE);
    	resultDto.setMessage(message);
    	return resultDto;
    }
    
    public Identity getLoginUser(HttpServletRequest request) {
    	return (Identity)request.getSession().getAttribute("identity");
    }
    
    public void warpModifyInfo(BaseDto dto, HttpServletRequest request){
    	Identity identity = getLoginUser(request);
		dto.setModifyBy(identity.getUserSn().toString());
    	dto.setModifyDate(new Date());
    }
    
    public void warpModifyInfoByName(BaseDto dto, HttpServletRequest request){
    	Identity identity = getLoginUser(request);
    	dto.setModifyBy(identity.getUserName());
    	dto.setModifyDate(new Date());
    }
    
    public void warpModifyInfo(BaseDto dto, HttpServletRequest request, Boolean create){
    	Identity identity = getLoginUser(request);
		dto.setModifyBy(identity.getUserSn().toString());
    	dto.setModifyDate(new Date());
    	if (create) {
			dto.setCreateBy(identity.getUserSn().toString());
			dto.setCreateDate(dto.getModifyDate());
		}
    }
    
    public void warpModifyInfoByName(BaseDto dto, HttpServletRequest request, Boolean create){
    	Identity identity = getLoginUser(request);
    	dto.setModifyBy(identity.getUserName());
    	dto.setModifyDate(new Date());
    	if (create) {
    		dto.setCreateBy(identity.getUserName());
    		dto.setCreateDate(dto.getModifyDate());
    	}
    }
    
    public HttpServletRequest getRequest() {
		return request;
	}
}
